package com.java.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.mapper.JobMapper;
import com.java.po.Job;
import com.java.po.JobMap;
import com.java.service.JobService;
import com.java.tool.AjaxJson;

public class JobServiceImpl implements JobService{

	@Autowired
	private JobMapper jobMapper;
	private AjaxJson ajaxJson;
	
	
	@SuppressWarnings("resource")
	public JobServiceImpl(){
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
		jobMapper = (JobMapper) applicationContext.getBean("jobMapper");		
		ajaxJson = new AjaxJson();
	}
	
	public void cleanAjaxJson(){
		ajaxJson.setData("");
		ajaxJson.setTotal(0);
		/*ajaxJson.setJob("");*/
	}
	@Override
	public AjaxJson insertJob(Job job) throws Exception {
		// TODO Auto-generated method stub
	
		int flag = jobMapper.insertJob(job);
		if(flag!=1){
			ajaxJson.setMessage("fault");
			return ajaxJson;
		}
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

	@Override
	public AjaxJson deleteJobById(Job job) throws Exception {
		// TODO 自动生成的方法存根
				cleanAjaxJson();
				// TODO 自动生成的方法存根
				int flag = jobMapper.deleteJob(job);
				if(flag!=1){
					ajaxJson.setMessage("fault");
					return ajaxJson;
				}
				ajaxJson.setMessage("success");
				return ajaxJson;
	}


	@Override
	public AjaxJson findAllJob(JobMap jobMap) throws Exception {
		// TODO 自动生成的方法存根
				cleanAjaxJson();
				// TODO 自动生成的方法存根
				Job job =new Job();
				int page = jobMap.getPage();
				int pageSize = jobMap.getPageSize();
				if(jobMap.getJob()!=null){
					job = jobMap.getJob();
				}
				PageHelper.startPage(page,pageSize);
				List<Job> list = jobMapper.findAllJob(job);
				PageInfo<Job> pageInfo = new PageInfo<>(list);
				ajaxJson.setData(pageInfo.getList());
				ajaxJson.setTotal((int)pageInfo.getTotal());
				return ajaxJson;
	}

	@Override
	public AjaxJson findJobByid(Job job) throws Exception {
		// TODO 自动生成的方法存根
				cleanAjaxJson();
				// TODO 自动生成的方法存根
				job =jobMapper.findJobByid(job);
			    ajaxJson.setData(job);
				ajaxJson.setTotal(1);
				ajaxJson.setMessage("success");
				return ajaxJson;
	}
	
	@Override
	public AjaxJson findJobSalary(Job job) throws Exception {
		// TODO 自动生成的方法存根
				cleanAjaxJson();
				// TODO 自动生成的方法存根
				job =jobMapper.findJobSalary(job);
			    ajaxJson.setData(job);
				ajaxJson.setTotal(1);
				ajaxJson.setMessage("success");
				return ajaxJson;
	}

	

	@Override
	public AjaxJson updateJobById(Job job) throws Exception {
		// TODO Auto-generated method stub
		int flag = jobMapper.updateJobByJobid(job);
		if(flag!=1){
			ajaxJson.setMessage("fault");
			return ajaxJson;
		}
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

}
